function [est_bits,ite,x_data] = MRC_detector(N,M,M_data,M_mod,no,data_grid,r,H_tf,L_set,omega,decision,init_estimate,n_ite_MRC,K_ml,Y)
% 傅里叶矩阵
Fn=dftmtx(N); 
Fn=Fn./norm(Fn);  
% 初始化
N_syms_perfram=sum(sum((data_grid>0)));
data_array=reshape(data_grid,1,N*M);
[~,data_index]=find(data_array>0);
M_bits=log2(M_mod);
N_bits_perfram = N_syms_perfram*M_bits;
Y_tilda=reshape(r,M,N);
M_prime=M_data;
L_set=L_set+1;
% 时频域上LMMSE均衡
if(init_estimate==1)
    Y_tf=fft(Y_tilda).'; %转化为时频域                                    %63
    X_tf=conj(H_tf).*Y_tf./(H_tf.*conj(H_tf)+no); %均衡                  %64
    X_est = ifft(X_tf.')*Fn; % 转化为延迟多普勒域                         %65
    X_est=qammod(qamdemod(X_est,M_mod,'gray'),M_mod,'gray');
    X_est=X_est.*data_grid;
else
    X_est=zeros(M,N);
end
x_m_hat=X_est.';

% MRC detector  
% 初始化
D_m=zeros(N,N,M);
D_m_inv=zeros(N,N,M);
y_m=Y.';
% 计算Dm
for m=1:M_prime
    for l=L_set
        D_m(:,:,m)=D_m(:,:,m)+K_ml(:,:,m+l-1,l)'*K_ml(:,:,m+l-1,l);  % 47
    end
    D_m_inv(:,:,m)=inv(D_m(:,:,m));
end
c_m=x_m_hat;
b_m_l=zeros(N,M,length(L_set));
%% 迭代
for ite=1:n_ite_MRC
    %计算gm
    g_m=zeros(N,M);
    RNPI_error=y_m;
    %计算bml
    for m=1:M_prime
        for l=L_set
            b_m_l(:,m,l)=y_m(:,m+l-1);
            for p=L_set
                if(l~=p)
                    if(m+(l-p)>0)
                        b_m_l(:,m,l)=b_m_l(:,m,l)-K_ml(:,:,m+l-1,p)*x_m_hat(:,m+(l-p));                               %5
                    end
                end
            end
            g_m(:,m)=g_m(:,m)+K_ml(:,:,m+(l-1),l)'*b_m_l(:,m,l);                                                      %7
        end
        %计算cm
        c_m(:,m)=D_m_inv(:,:,m)*g_m(:,m);                                                                             % 8
        if(decision==1)
            x_m_hat(:,m)=(1-omega)*c_m(:,m)+omega*qammod(qamdemod((c_m(:,m)),M_mod,'gray'),M_mod,'gray');             % 9
        else
            x_m_hat(:,m)=c_m(:,m);
        end
        %计算残余噪声加干扰
        for l=L_set
            if(m>=l)
                RNPI_error(:,m)=RNPI_error(:,m)-reshape(K_ml(:,:,m,l),N,N)*x_m_hat(:,m-(l-1));                         % 50
            end
        end
    end
    % 收敛准则
    error(ite)=norm(RNPI_error);
    if(ite>1)
        % 错误变大就退出
        if(error(ite)>=error(ite-1))
            break;
        end
    end
end
if(n_ite_MRC==0)
    ite=0;
end
%% 检测输出
X_est=x_m_hat.';
x_est=reshape(X_est,1,N*M);
x_data=x_est(data_index);
%qam解调
est_bits=reshape(qamdemod(x_data,M_mod,'gray','OutputType','bit'),N_bits_perfram,1);
end
